// axios 
import axios from 'axios'
import {Toast} from 'vant'

// axios.defaults.baseURL = "http://157.122.54.189:9083"
axios.defaults.baseURL = "http://liangwei.tech:3000"

axios.interceptors.request.use(config => {

  if(!config.headers.Authorization && localStorage.getItem('token')) {
    config.headers.Authorization = localStorage.getItem('token')
  }

  return config
})
axios.interceptors.response.use(res => {
  console.log('路过拦截器, 直接将数据返回');
  const errCodePattern = /^4\d{2}$/
  if(errCodePattern.test(res.data.statusCode)) {
    Toast.fail(res.data.message || '系统错误')
  }

  if(res.data.message == "用户信息验证失败") {
    localStorage.removeItem("token");
    localStorage.removeItem("userId");
    router.replace('/login')
  }

  return res
})

export default axios